{% extends 'layout_index.html' %}

{% load static %}

{% block title %}
    <title>班级信息</title>
{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/class_info.css' %}">
{% endblock %}

{% block main_content %}
<div class="class-info-container">
    <div class="class-info-title">
        <h3>软件工程学院班级信息</h3>
    </div>

    <!-- 顶部操作栏 -->
    <div class="operation-bar">
        <!-- 搜索和筛选区域 -->
        <form method="get" action="{% url 'class_list' %}" class="search-form">
            <div class="search-area">
                <div class="search-group">
                    <input type="text" name="search" class="form-control" placeholder="搜索班级名称..." value="{{ request.GET.search }}">
                    <select name="grade" class="form-control">
                        <option value="">选择年级</option>
                        {% for grade in grades %}
                        <option value="{{ grade.grade_id }}" {% if selected_grade == grade.grade_id|stringformat:"s" %}selected{% endif %}>
                            {{ grade.get_grade_id_display }}
                        </option>
                        {% empty %}
                            <option value="">暂无年级</option>
                        {% endfor %}
                    </select>
                    <button type="submit" class="btn btn-info">
                        <i class="fa-solid fa-search"></i> 搜索
                    </button>
                </div>
            </div>
        </form>

        <!-- 添加班级按钮 -->
        <div class="add-button">
            <a href="{% url 'add_class' %}?type=class" class="btn btn-success">
                <i class="fa-solid fa-plus"></i> 添加班级
            </a>
        </div>
    </div>

    <!-- 班级列表 -->
    <div class="class-list-container">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th width="10%">序号</th>
                        <th width="20%"><i class="fa-solid fa-building-columns"></i> 年级</th>
                        <th width="20%"><i class="fa-solid fa-graduation-cap"></i> 班级</th>
                        <th width="20%"><i class="fa-solid fa-hashtag"></i> 班级号</th>
                        <th width="15%"><i class="fa-solid fa-users"></i> 班级人数</th>
                        <th width="15%"><i class="fa-solid fa-gear"></i> 操作</th>
                    </tr>
                </thead>
                <tbody id="classTableBody">
                    {% for class in classes %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ class.grade_id.get_grade_id_display }}</td>
                        <td>{{ class.class_name }}</td>
                        <td>{{ class.class_id }}</td>
                        <td>{{ class.student_count }}</td>
                        <td>
                            <div class="btn-group">
                                <a href="{% url 'update_class' class_id=class.class_id %}" class="btn btn-info btn-sm">
                                    <i class="fa-solid fa-pencil"></i> 修改
                                </a>
                                <button class="btn btn-danger btn-sm delete-class" data-class-id="{{ class.class_id }}">
                                    <i class="fa-solid fa-trash"></i> 删除
                                </button>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-center">暂无班级信息</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    <div>

    <!-- 分页实现 -->
    <div class="pagination-container">
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <!-- 首页 -->
                <li class="page-item {% if not classes.has_previous %}disabled{% endif %}">
                    <a class="page-link" href="?page=1">首页</a>
                </li>
                <!-- 上一页 -->
                <li class="page-item {% if not classes.has_previous %}disabled{% endif %}">
                    <a class="page-link" href="{% if classes.has_previous %}?page={{ classes.previous_page_number }}{% else %}javascript:;{% endif %}">
                        <i class="fa fa-angle-left"></i>
                    </a>
                </li>

                <!-- 页码 -->
                {% for page in classes.paginator.page_range %}
                    {% if page == classes.number %}
                        <li class="page-item active">
                            <span class="page-link">{{ page }}</span>
                        </li>
                    {% elif page > classes.number|add:'-3' and page < classes.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page }}">{{ page }}</a>
                        </li>
                    {% endif %}
                {% endfor %}

                <!-- 下一页 -->
                <li class="page-item {% if not classes.has_next %}disabled{% endif %}">
                    <a class="page-link" href="{% if classes.has_next %}?page={{ classes.next_page_number }}{% else %}javascript:;{% endif %}">
                        <i class="fa fa-angle-right"></i>
                    </a>
                </li>
                <!-- 末页 -->
                <li class="page-item {% if not classes.has_next %}disabled{% endif %}">
                    <a class="page-link" href="?page={{ classes.paginator.num_pages }}">末页</a>
                </li>
            </ul>
        </nav>
        <!-- 分页信息 -->
        <div class="text-center">
            <span class="text-muted small">
                第 {{ classes.number }}/{{ classes.paginator.num_pages }} 页，共 {{ classes.paginator.count }} 条记录
            </span>
        </div>
    </div>

</div>
{% endblock %}

{% block js %}
<script>
$(document).ready(function() {
    // 删除班级
    $('.delete-class').click(function(e) {
        e.preventDefault();
        var classId = $(this).data('class-id');
        
        if (confirm('确定要删除这个班级吗？')) {
            $.ajax({
                url: "/index/class/delete/" + classId + "/",
                type: "POST", 
                data: {
                    'csrfmiddlewaretoken': '{{ csrf_token }}'
                },
                success: function(response) {
                    if (response.status === 'success') {
                        alert('删除成功');
                        location.reload();
                    } else {
                        alert('删除失败：' + response.message);
                    }
                },
                error: function() {
                    alert('系统错误，删除失败');
                }
            });
        }
    });
});
</script>
{% endblock %}


